import os
# 修复SSL权限错误 - 禁用SSL密钥日志
os.environ['SSLKEYLOGFILE'] = ''

import dashscope
from dashscope.api_entities.dashscope_response import Role
import os
# 从环境变量中获取 API Key
dashscope.api_key = "sk-f648b6ff7a0b40999edc8c15ec81c459"

# 封装模型响应函数
def get_response(messages):
    response = dashscope.Generation.call(
        model='deepseek-r1',  # 使用 deepseek-r1 模型
        messages=messages,
        result_format='message'  # 将输出设置为message形式
    )
    return response

# 测试对话
messages = [
    {"role": "system", "content": "You are a helpful assistant"},
    {"role": "user", "content": "你好，你是什么大模型？"}
]
response = get_response(messages)
# 添加错误检查
if response.status_code == 200 and response.output:
    print(response.output.choices[0].message.content)
else:
    print(f"请求失败，状态码: {response.status_code}, 错误信息: {response}")

# 情感分析
review = '这款音效特别好 给你意想不到的音质。'
messages = [
    {"role": "system", "content": "你是一名舆情分析师，帮我判断产品口碑的正负向，回复请用一个词语：正向 或者 负向"},
    {"role": "user", "content": review}
]

response = get_response(messages)
# 添加错误检查
if response.status_code == 200 and response.output:
    print(response.output.choices[0].message.content)
else:
    print(f"请求失败，状态码: {response.status_code}, 错误信息: {response}")